﻿#include"Snake.h"
#define _CRT_SECURE_NO_WARNINGS 1
void SetPos(short x, short y)
{
	HANDLE handle = NULL;
	handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x,y };
	SetConsoleCursorPosition(handle, pos);
}
//初始设置
void SetInitial()
{
	system("mode con cols=100 lines=35");
	system("title 贪吃蛇");
	HANDLE handle = NULL;
	//获取标准输出的句柄(用来标识不同设备的数值)
	handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false; //隐藏控制台光标
	SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
//打印欢迎语
void Welcome()
{
	SetPos(32, 13);
	printf("◆◆◆欢迎来到贪吃蛇游戏◆◆◆");
	SetPos(38, 28);
	system("pause");
	system("cls");
	SetPos(36, 13);
	printf("用↑↓←→来控制蛇的移动");
	SetPos(40, 14);
	printf("F3为加速，F4为减速");
	SetPos(40, 15);
	printf("加速会获得更高分数");
	SetPos(38, 28);
	system("pause");
	system("cls");
}
//地图绘制
void CreateMap()
{
	SetPos(0, 0);
	for (int i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	SetPos(0, 26);
	for (int i = 0; i < 58; i += 2)
	{
		wprintf(L"%lc", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	for (int i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}
//初始化蛇身
void InitSnake(pSnake ps)
{
	ps->Head = NULL;
	pSnakeNode NewNode = NULL;
	for (int i = 0; i < 5; i++)
	{
		NewNode = (pSnakeNode)malloc(sizeof(SnakeNode));
		NewNode->Next = NULL;
		NewNode->x = 20;
		NewNode->y = 6 + i;

		if (ps->Head == NULL)
		{
			ps->Head = NewNode;
		}
		else
		{
			NewNode->Next = ps->Head;
			ps->Head = NewNode;
		}
		SetPos(NewNode->x, NewNode->y);
		wprintf(L"%lc", BODY);
	}
	ps->Score = 0;
	ps->SleepTime = 140;
	ps->FoodScore = 10;
	ps->Dir = DOWN;
	ps->Status = OK;
	ps->FoodScore = 3;

}
//初始化食物
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;
again:
	do
	{
		x =rand() % 53 +2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);
	pSnakeNode cur = ps->Head;
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->Next;
	}
	pSnakeNode NewNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	NewNode->Next = NULL;
	NewNode->x = x;
	NewNode->y = y;
	SetPos(x, y);
	wprintf(L"★");
	ps->Food = NewNode;
}
void GameStare(pSnake ps)
{
	SetInitial();
	Welcome();
	CreateMap();
	InitSnake(ps);
	CreateFood(ps);
}
//游戏运行时的帮助信息
void HelpInfo()
{
	SetPos(65, 11);
	printf("用↑↓←→来控制蛇的移动");
	SetPos(65, 12);
	printf("不能撞墙，不能咬到自己");
	SetPos(65, 13);
	printf("F3为加速，F4为减速");
}
//蛇吃到食物
void EatFood(pSnake phead, pSnakeNode pnew)
{
	pnew->Next = phead->Head;
	phead->Head = pnew;
	pSnakeNode cur = phead->Head;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->Next;
	}
	phead->Score += phead->FoodScore;
	free(phead->Food);
	CreateFood(phead);
}
//蛇没吃到食物
void NotEatFood(pSnake phead, pSnakeNode pnew)
{
	pnew->Next = phead->Head;
	phead->Head = pnew;
	pSnakeNode cur = phead->Head;
	while (cur->Next->Next !=NULL)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->Next;
	}
	SetPos(cur->Next->x, cur->Next->y);
	printf("  ");
	free(cur->Next);
	cur->Next = NULL;
}
void KillByWall(pSnake ps)
{
	if (ps->Head->x == 0 || ps->Head->x == 56 || ps->Head->y == 0 || ps->Head->y == 26)
		ps->Status = KILL_BY_WALL;
}
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->Head;
	pSnakeNode next = ps->Head->Next;
	while (next)
	{
		if (next->x == cur->x && next->y == cur->y)
			ps->Status = KILL_BY_SELF;
		next = next->Next;
	}
}
//控制蛇的移动
void SnakeMove(pSnake ps)
{
	pSnakeNode NewNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	NewNode->Next = NULL;
	switch (ps->Dir)
	{
	case UP:
	{
		NewNode->x = ps->Head->x;
		NewNode->y = ps->Head->y - 1;
	}
	break;
	case DOWN:
	{
		NewNode->x = ps->Head->x;
		NewNode->y = ps->Head->y + 1;
	}
	break;
	case LEFT:
	{
		NewNode->x = ps->Head->x - 2;
		NewNode->y = ps->Head->y;
	}
	break;
	case RIGHT:
	{
		NewNode->x = ps->Head->x + 2;
		NewNode->y = ps->Head->y;
	}
	break;
	}
	if (NewNode->x == ps->Food->x && NewNode->y == ps->Food->y)
	{
		EatFood(ps, NewNode);
	}
	else
	{
		NotEatFood(ps, NewNode);
	}
	KillByWall(ps);
	KillBySelf(ps);
}
//游戏运行
void GameRun(pSnake ps)
{
	HelpInfo();
	do
	{
		SetPos(65, 10);
		printf("总分：%d分   每个食物得分：%02d分", ps->Score, ps->FoodScore);
		if (KEY(VK_UP) && ps->Dir != DOWN)
			ps->Dir = UP;
		else if (KEY(VK_DOWN) && ps->Dir != UP)
			ps->Dir = DOWN;
		else if (KEY(VK_LEFT) && ps->Dir != RIGHT)
			ps->Dir = LEFT;
		else if (KEY(VK_RIGHT) && ps->Dir != LEFT)
			ps->Dir = RIGHT;
		else if (KEY(VK_SPACE))
		{
			SetPos(68, 18);
			printf("游戏已暂停！");
			SetPos(66, 20);
			system("pause");
			SetPos(68, 18);
			printf("                   ");
			SetPos(66, 20);
			printf("                   ");
		}
		else if (KEY(VK_ESCAPE))
		{
			ps->Status = ESC;
			break;
		}
		else if (KEY(VK_F3))
		{
			if (ps->SleepTime > 50)
			{
				ps->SleepTime -= 30;
				ps->FoodScore += 2;
			}
		}
		else if (KEY(VK_F4))
		{
			if (ps->SleepTime < 140)
			{
				ps->SleepTime += 30;
				ps->FoodScore -= 2;
			}
			else if (ps->SleepTime < 230)
			{
				ps->SleepTime += 30;
				ps->FoodScore = 1;
			}
		}
		Sleep(ps->SleepTime);
		SnakeMove(ps);
	} while (ps->Status == OK);
}
void GameOver(pSnake ps)
{
	switch (ps->Status)
	{
	case KILL_BY_SELF:
	{
		system("cls");
		SetPos(36, 16);
		printf("蛇咬到自己游戏结束！");
	}
	break;
	case KILL_BY_WALL:
	{
		system("cls");
		SetPos(38, 16);
		printf("蛇撞到墙游戏结束！");
	}
	break;
	case ESC:
	{
		system("cls");
		SetPos(40, 16);
		printf("游戏已退出！");
	}
	break;
	}
	pSnakeNode p1 = ps->Head;
	pSnakeNode p2 = NULL;
	while (p1)
	{
		p2 = p1;
		p1 = p1->Next;
		free(p2);
	}
	free(ps->Food);
}
void PlayGame(pSnake ps)
{
	int ch = 0;
	do
	{
		GameStare(ps);
		GameRun(ps);
		GameOver(ps);
		SetPos(40, 17);
		printf("是否再来一局(Y/N):");
		ch = getchar();
		getchar();
	} while (ch == 'y' || ch =='Y');
}